package com.huicoo.forestmanager.util.audio;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.huicoo.forestmanager.R;

public class AudioRecordDialog extends Dialog implements View.OnClickListener {
    private TextView tv_title, tv_time, tv_btn;
    private ImageView iv_status;

    private AudioRecordUtils utils;
    private boolean isRecording;
    private int maxSeconds = 60;

    private OnAudioRecordCompleteListener onAudioRecordCompleteListener;

    public AudioRecordDialog(Context context) {
        super(context, R.style.VoiceDialog);
        setContentView(R.layout.dialog_audio_record);

        setCancelable(false);
        setCanceledOnTouchOutside(false);

        initUtils();
        initView();

    }

    private void initUtils() {
        utils = new AudioRecordUtils();
        utils.setMaxLength(60);
        utils.setOnAudioStatusUpdateListener(new AudioRecordUtils.OnAudioStatusUpdateListener() {
            @Override
            public void onUpdate(double db, long time) {
                tv_time.setText(String.format("剩余%s''", String.valueOf(maxSeconds - time / 1000)));
            }

            @Override
            public void onStop(String filePath, int during) {
                if (onAudioRecordCompleteListener != null) {
                    onAudioRecordCompleteListener.recordComplete(filePath, during);
                }
            }
        });
    }

    private void initView() {
        tv_title = findViewById(R.id.tv_title);
        tv_time = findViewById(R.id.tv_time);
        iv_status = findViewById(R.id.iv_status);
        tv_btn = findViewById(R.id.tv_btn);

        View close = findViewById(R.id.close);
        close.setOnClickListener(this);
        findViewById(R.id.btn).setOnClickListener(this);
    }

    public void setMaxSeconds(int seconds) {
        this.maxSeconds = seconds;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.close:
                if (isRecording) {
                    utils.cancelRecord();
                }
                dismiss();
                break;
            case R.id.btn:
                isRecording = !isRecording;
                checkRecordStatus();
                break;
            default:
                break;
        }
    }

    private void checkRecordStatus() {
        tv_time.setText("");
        if (isRecording) {
            utils.startRecord();
            tv_title.setText("正在录音");
            tv_btn.setText("点击停止");
        } else {
            utils.stopRecord();
            tv_title.setText("请点击麦克风说话");
            tv_btn.setText("点击开始");
        }
    }

    public interface OnAudioRecordCompleteListener {
        void recordComplete(String filePath, int during);
    }

    public void setOnAudioRecordCompleteListener(OnAudioRecordCompleteListener onAudioRecordCompleteListener) {
        this.onAudioRecordCompleteListener = onAudioRecordCompleteListener;
    }
}
